//
//  MainTabBarController.m
//  HWSGW
//
//  Created by avachen on 15/10/31.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "MainTabBarController.h"
#import "HWTabBarItem.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg_all-64"] forBarMetrics:UIBarMetricsDefault];
    
    
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
//需要在viewWillAppear方法中调用，因为用故事版创建视图的话会在移除之后重新添加上去
- (void)viewWillAppear:(BOOL)animated
{
    //先调用父类的viewWillAppear方法
    [super viewWillAppear:animated];
    [self _createTabbar];
    
}
- (void)_createTabbar{
  
    //从tabbar中拿到所有的视图
    for (UIView *view in self.tabBar.subviews) {
//        NSLog(@"%@", view);
        
        //Class是一个抽象类
        //视图1、视图2  --->视图类
        //视图类、按钮类、图片类 --->类
        //选出想要删得UITabBarButton类型的视图
        Class cls = NSClassFromString(@"UITabBarButton");
        //判断
        if ([view isKindOfClass:cls]) {
            
            //删除UITabBarButton视图
            [view removeFromSuperview];
        }
    }
    NSArray *imgArray = @[@"movie_home.png", @"msg_new.png", @"start_top250.png", @"icon_cinema.png", @"more_setting.png"];
    
    NSArray *titleArray = @[@"电影", @"新闻", @"top", @"影院", @"更多"];

    
    CGFloat width = kScreenWidth / imgArray.count;
    
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    
    if (_selectImg == nil) {
        
        //创建点击遮盖视图
        _selectImg = [[UIImageView alloc] initWithFrame:CGRectMake(7, 0, 50, 45)];
        //添加图片
        _selectImg.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
        //添加到tabbar
        [self.tabBar addSubview:_selectImg];
    }
    
    
           //遍历数组方式来创建button
    for (int i = 0; i < imgArray.count; i++) {
        
        NSString *title = titleArray[i];
        NSString *img = imgArray[i];
//        
//        //创建按钮
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(i * width, 0, width, height);
//        
//        //创建标题
//        [button setTitle:title forState:UIControlStateNormal];
//        //创建图片
//        [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
//        //设置标题字体
//        button.titleLabel.font = [UIFont systemFontOfSize:11];
//        
//        //设置标题和图片偏移，成为上下格式
//        button.titleEdgeInsets = UIEdgeInsetsMake(30, -20, 0, 0);
//        button.imageEdgeInsets = UIEdgeInsetsMake(-15, 20, 0, 0);
//        
//        [self.tabBar addSubview:button];
        CGRect frame = CGRectMake(i * width, 0, width, height);
        //使用子类化的Item创建标签
        HWTabBarItem *item = [[HWTabBarItem alloc] initWithFrame:frame imageName:img title:title];
        item.tag = i;
        //添加点击事件
        [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.tabBar addSubview:item];
        
        if (i == 0 && item.selected == YES) {
            _selectImg.center = item.center;
            
        }
    }

    
    
    
}
//点击事件
- (void)clickItem:(HWTabBarItem *)item{
    self.selectedIndex = item.tag;
    //添加动画效果，使遮盖视图点击响应移动
    [UIView animateWithDuration:0.2 animations:^{
        
        _selectImg.center = item.center;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
